//
//  ScanView.m
//  Learn
//
//  Created by chijianguo on 2024/4/8.
//  Copyright © 2024 迟建国. All rights reserved.
//

#import "ScanView.h"

@interface ScanView ()<UITextFieldDelegate>

@end


@implementation ScanView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    self.bgView = [[UIView alloc] init];
    self.bgView.backgroundColor = COLOR_WHITE;
    self.bgView.layer.cornerRadius = 20;
    self.bgView.layer.masksToBounds = YES;
    [self addSubview:self.bgView];
    
    self.searchImBtn = [[UIButton alloc] init];
    [self.searchImBtn setImage:[UIImage imageNamed:@"icon_search"] forState:(UIControlStateNormal)];
    [self addSubview:self.searchImBtn];
    
    self.textFD = [[UITextField alloc] init];
    self.textFD.placeholder = @"请输入搜索内容";
    [self.textFD addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    self.textFD.delegate = self;
    self.textFD.font = [UIFont systemFontOfSize:20];
    self.textFD.tintColor = COLOR_b5b5b5;
    [self addSubview:self.textFD];
    
    self.searchBtn = [[UIButton alloc] init];
    [self.searchBtn setTitle:@"搜索" forState:(UIControlStateNormal)];
    self.searchBtn.backgroundColor = rgbHex(0x09CC76, 1);
    self.searchBtn.layer.cornerRadius = 18;
    self.searchBtn.layer.masksToBounds = YES;
    [self addSubview:self.searchBtn];
    [self.searchBtn addTarget:self action:@selector(searchAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self).offset(0.0);
        make.left.equalTo(self).offset(10.0);
        make.right.equalTo(self).offset(-10);
        make.height.mas_equalTo(40);
    }];
    [self.searchImBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgView).offset(10.0);
        make.centerY.mas_equalTo(self.bgView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(16, 16));
    }];
    [self.textFD mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.bgView.mas_centerY);
        make.left.equalTo(self.searchImBtn.mas_right).offset(10.0);
        make.height.mas_equalTo(40);
    }];
    [self.searchBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bgView).offset(2.0);
        make.right.bottom.equalTo(self.bgView).offset(-2);
        make.width.mas_equalTo(64);
    }];

}


- (void)searchAction:(UIButton *)sender {
    [self.textFD resignFirstResponder];

    if (self.searchBlock) {
        self.searchBlock();
    }
}


#pragma mark -----  输入框代理方法
//限制输入内容为字母和数字，限制长度
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    return YES;
}

//输入框输入监听
- (void)textFieldDidChange:(UITextField *) sender {
    if (self.searchTextBlock) {
        self.searchTextBlock(sender.text);
    }
}

//键盘return建退出键盘
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    [self.textFD resignFirstResponder];
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    
}

@end
